# 创建一个浏览器对象
import datetime
import os.path
import sys

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service



# class Driver:
#     driver = ""
#     def __init__(self):
#         options = webdriver.ChromeOptions()
#         self.driver = webdriver.Chrome(service= Service(ChromeDriverManager().install()), options=options)

class Driver:
    driver = ""
    def __init__(self):
        chrome_driver_path = 'G:\浏览器下载\chromedriver-win64\chromedriver-win64\chromedriver.exe'
        service = Service(chrome_driver_path)
        options = Options()
        options.add_argument(
            "--disable-blink-features=AutomationControlled")  # 作用：禁用 Blink 引擎的自动化控制检测，用于隐藏自动化特征，防止网站识别 Selenium
        options.add_experimental_option("excludeSwitches",
                                        ["enable-automation"])  # 作用：排除 "enable-automation" 开关  会隐藏浏览器顶部的 "正受到自动测试软件控制" 提示
        options.add_experimental_option('useAutomationExtension', False)  # 作用: 禁用自动化扩展  进一步防止被检测为自动化工具
        self.driver = webdriver.Chrome(service=service, options=options)
        self.driver.implicitly_wait(2)
    def getScreeShot(self):
        #创建屏幕截图
        #图片文件名称：2025-05-17-210940.png
        #图片路径: ../images/2025-05-17/调用方法-2025-05-17-210940.png
        dirname = datetime.datetime.now().strftime("%Y-%m-%d")
        #判断dirname文件夹是否已经存在，若不存在则创建文件夹
        if not os.path.exists("../images/"+dirname):
            os.mkdir("../images/"+dirname)
        filename = sys._getframe().f_back.f_code.co_name+"-"+datetime.datetime.now().strftime("%Y-%m-%d-%H%M%S")+".png"
        self.driver.save_screenshot("../images/"+dirname+"/"+filename)

BlogDriver = Driver()
